package com.guiji.quartz.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import cn.wanghaomiao.xpath.exception.NoSuchAxisException;
import cn.wanghaomiao.xpath.exception.NoSuchFunctionException;
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guiji.quartz.domain.*;
import com.guiji.quartz.mapper.SpiderNewsMapper;
import com.guiji.quartz.mapper.SpiderNewsStatMapper;
import com.guiji.quartz.mapper.SysJobMapper;
import com.guiji.quartz.service.ISpiderNewsService;
import com.guiji.quartz.util.DelPicUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.model.ConsolePageModelPipeline;
import us.codecraft.webmagic.model.OOSpider;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName WebmagicTask
 * @Description TODO
 * @Author 钟维澎（四月）
 * @Date 2021/7/14 9:49
 * @Version 1.0
 */
@Component("webMagicTask")
public class WebMagicTask {
	private static Logger logger = LoggerFactory.getLogger(WebMagicTask.class);
	@Autowired
	private ISpiderNewsService spiderNewsService;
	@Autowired
	SysJobMapper sysJobMapper;

	private Snowflake snowflake = IdUtil.getSnowflake(1, 1);


	public void deleteNews(Integer day) {
		Long startTime = System.currentTimeMillis();
		QueryWrapper<SpiderNews> queryWrapper = new QueryWrapper<>();
		queryWrapper.lt("create_time", DateUtil.offsetDay(new Date(), -1 * day))
			.or()
			.lt("news_public_time", DateUtil.offsetDay(new Date(), -1 * day));
		//获取列表
		List<SpiderNews> list = spiderNewsService.list(queryWrapper);
		try {
			sysJobMapper.bulkInsert(list);
		} catch (Exception e) {
			logger.info("insertNews-error-:{}",e.toString());
		}
		//插入失败，也会进行删除数据
		spiderNewsService.remove(queryWrapper);
		logger.info("deleteNews-end-:{}", System.currentTimeMillis() - startTime);
	}

	public void sinaNews() {
		Long startTime = System.currentTimeMillis();
		Spider spider = OOSpider.create(Site.me().setSleepTime(1000), new ConsolePageModelPipeline(), SinaNews.class)
			.addUrl("https://tech.sina.com.cn/")
			.thread(20);
		spider.run();
		logger.info("sinaNews-end-:{}", System.currentTimeMillis() - startTime);
	}

}
